package com.example.bbs.web.servlet; /**
 * @auther: zhangxin
 * @Description:
 */

import com.example.bbs.model.User;
import com.example.bbs.service.UserService;
import com.example.bbs.service.impl.UserServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "CheckRepeatServlet", value = "/chackRepeatName")
public class CheckRepeatServlet extends HttpServlet {
    UserService userService = new UserServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        String json = null;
        String userName = req.getParameter("userName");
        if(userName.equals("")){
            json = "{\"resp\":\"请填写用户名\"}";
            resp.getWriter().write(json);
            return;
        }
        User user = new User(userName);
        user = userService.findUserByName(user);
        if(user != null){
            json = "{\"resp\":\"用户名重复\"}";
        }else{
            json = "{\"resp\":\"\"}";
        }
        resp.getWriter().write(json);
    }

}
